package net.ojbk.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ojbk.service.ActivationService;

/**
 * Servlet implementation class ActivationServlet
 */
@WebServlet("/home/ActivationServlet")
public class ActivationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActivationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// user activation
		// 1、get Parameter
		String code = request.getParameter("code");
		
		// 2、init Object
		ActivationService activService = new ActivationService();
		PrintWriter out = response.getWriter();
		int statusCode;
		String str = "";
		
		// 3、1：成功 2：code错误或已激活
		statusCode = activService.activation(code);
		switch (statusCode) {
		case 1:
			str = "Activate the success, the account you can use."; break;
		case 2:
			str = "code error or url error！"; break;
		}
		
		// 4、write
		out.write(str);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// user register
		// 1、get Parameter
		String password = request.getParameter("password");
		String e = request.getParameter("e");
		String identifyCode = request.getParameter("identifyCode");
		
		// 2、init Object
		ActivationService activService = new ActivationService();
		PrintWriter out = response.getWriter();
		String str = "";
		
		// 3、1：成功 2：密码不符合要求 3：Email已被注册
		if (!identifyCode.equals("AAAAAA")) {
			out.write("IdentifyCode is error."); return ;
		}
		int statusCode = activService.register(e, password);
		switch (statusCode) {
		case 1:
			str = "register was successful. Please check the email to be activated."; break;
		case 2:
			str = "Password does not meet the requirements."; break;
		case 3:
			str = "E - mail has been registered."; break;
		}
		
		// 4、write
		out.write(str);
	}

}
